
#include <stdbool.h>
// #include <pins_arduino.h>
#include "Arduino.h"

// ***********************************
// This section define some constant for
// controling some firmware feature
// ***********************************

// Ptt pin number
#define pttPin 8

// Mic pin number
#define micPin 10

// Led pin number
#define ledPin 13

// Tone frequency
#define toneFrequency (800)



// ***********************************
// End section
// ***********************************
// 
// This code is so trivial that I'm releasing it completely without 
// restrictions.  If you find it useful, it would be nice if you dropped
// me an email, maybe plugged my blog @ http://brainwagon.org or included
// a brief acknowledgement in whatever derivative you create, but that's
// just a courtesy.  Feel free to do whatever.
//


struct t_mtab { char c, pat; } ;

static struct t_mtab morsetab[] = {
  	{'.', 106},
	{',', 115},
	{'?', 76},
	{'/', 41},
	{'A', 6},
	{'B', 17},
	{'C', 21},
	{'D', 9},
	{'E', 2},
	{'F', 20},
	{'G', 11},
	{'H', 16},
	{'I', 4},
	{'J', 30},
	{'K', 13},
	{'L', 18},
	{'M', 7},
	{'N', 5},
	{'O', 15},
	{'P', 22},
	{'Q', 27},
	{'R', 10},
	{'S', 8},
	{'T', 3},
	{'U', 12},
	{'V', 24},
	{'W', 14},
	{'X', 25},
	{'Y', 29},
	{'Z', 19},
	{'1', 62},
	{'2', 60},
	{'3', 56},
	{'4', 48},
	{'5', 32},
	{'6', 33},
	{'7', 35},
	{'8', 39},
	{'9', 47},
	{'0', 63},

        // Added TO TEST decode fldigi
        {'&', 52},
        {'=', 49},
        
} ;

#define N_MORSE  (sizeof(morsetab)/sizeof(morsetab[0]))

// #define SPEED  (12)
#define SPEED  (20)
#define DOTLEN  (1200/SPEED)
#define DASHLEN  (3*(1200/SPEED))


// This function drive the PTT
// If parIsActive == false, PTT is not active, otherwise it's active
void DrivePtt(bool parIsActive)
{
  if(parIsActive == false)
  { // Not high because 3,3 come from arduino not from Tx
    pinMode(pttPin, INPUT);  
    
  }
  else
  { // Be sure that GND of Tx is connected to Arduino Tx
    pinMode(pttPin, OUTPUT);      
  }
}

// Tone generation on
void GenerateTone(boolean parIsActive)
{
  if(parIsActive == false)
  { 
    noTone(micPin);
    digitalWrite(ledPin, LOW);
  }
  else
  {    

    digitalWrite(ledPin, HIGH);
    // play a note on pin Mic
    tone(micPin, toneFrequency);
  }
}


void dash(void)
{
  GenerateTone(true);
  delay(DASHLEN);
  GenerateTone(false);
  delay(DOTLEN) ;
}

void dit(void)
{
  GenerateTone(true);
  delay(DOTLEN);
  GenerateTone(false);
  delay(DOTLEN);
}

void send(char c)
{
  int i;
  /*
Serial.print("Send char :");
  Serial.print(c);
  Serial.print("\n");
  */
  c = toupper(c);
  if (c == ' ') 
  {
    // Serial.print(c) ;
    delay(7*DOTLEN) ;
    return ;
  }
  
  for (i=0; i<N_MORSE; i++) 
  {
    if (morsetab[i].c == c) 
    {
      unsigned char p = morsetab[i].pat ;
      //Serial.print(morsetab[i].c) ;

      while (p != 1) 
      {
          if (p & 1)
            dash() ;
          else
            dit() ;
          p = p / 2 ;
      }
      delay(2*DOTLEN) ;
      return ;
    }
  }
  
  /* if we drop off the end, then we send a space */
  //Serial.print("$") ;
}


void MORSE_SendMsg(char *str)
{
  //Serial.print(str);
  DrivePtt(true);
  delay(1000);
  
  while (*str)
    send(*str++);
    
  // Serial.println("");
  
  delay(1000);
  DrivePtt(false);
}




